package com.wifi.filter;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter("/*")
public class LoginFilter implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        //用户必须登录 不登陆返回
        HttpServletRequest request1=(HttpServletRequest)request;
        HttpServletResponse response1=(HttpServletResponse)response;

      Object o=request1.getSession().getAttribute("user");
      String url=request1.getRequestURI();
      if (url.endsWith("login.jsp")||o!=null
              ||url.endsWith(".js")||url.endsWith(".css")
              ||url.endsWith(".jpg")||url.endsWith(".png")
              ||url.endsWith(".ico")||url.endsWith(".map")
      ||url.endsWith("login")||url.endsWith(".svg")
          ||url.endsWith("getPwd")||url.endsWith("signup"))
      {chain.doFilter(request1, response1);}
      else {
          response1.sendRedirect(request1.getContextPath()+"/login.jsp");
      }

    }
}
